<?php
/**
 * created by zzy
 * date: 2017/11/30 16:48
 */
namespace App\Api\Service\Forms\Controllers;

use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\OrderProcessCourse;
use App\Engine\Func;

class OrderController extends Controller
{
    public function course()
    {
        $userId                = Func::getHeaderValueByName( 'userid' );
        $info                   = DepartmentUser::getCurrentInfo( $userId );

        $pageSize               = request( 'pageSize', 10 );
        $page                   = request( 'page', 1 );
        $offset                 = ( $page - 1 ) * $pageSize;

        $startDate             = request( 'start_date', '' );
        $endDate               = request( 'end_date', '' );
        if( !$info ){
            xThrow( 10006 );
        }
        $startTime             = Func::getFormsDateTime( $startDate );
        $endTime               = Func::getFormsDateTime( $endDate, true );
        $companyId             = $info->company_id;

        $where                          = ['company_id'=>$companyId];
        $departUserListCollection    = DepartmentUser::getList( $where );
        $departUserArr                = $departUserListCollection->pluck('truename', 'user_id')->toArray();
        $departUserPrice              = $departUserListCollection->pluck('price', 'user_id')->toArray();
        $whereArr                      = [
            'company_id'=>$companyId, 'start_time'=>$startTime,'end_time'=>$endTime,
        ];
        $formsData                     = OrderProcessCourse::getFormsData( $whereArr, $pageSize, $offset );
        $count                          = $formsData['count'];
        $courseListCollection         = $formsData['lists'];
        $formsMaxData                 = OrderProcessCourse::getFormsData( $whereArr );
        $courseMaxNumber              = 0;
        if( $formsMaxData )
        {
            $courseMaxNumber          = $formsMaxData->finish_number_sum;
        }
        $lists                          = $userIdArr = [];
        $orderNumber                   = 0;
        foreach( $courseListCollection as $key=>$val)
        {
            $valUid                    = $val->uid;
            $finishNumberSum          = $val->finish_number_sum;
            $percentNum                = ($finishNumberSum/$courseMaxNumber) * 100;
            $percentRound              = round( $percentNum );
            $percent                    = $percentRound/100;
            $truename                   = $departUserArr[$valUid];
            $price                      = $departUserPrice[$valUid];
            $totalPrice                = $finishNumberSum * $price;
            $arr                        = [
                'user_id'=>$valUid,'truename'=>$truename,
                'number'=>$finishNumberSum,'total_price'=>$totalPrice,
                'percent'=>$percent, 'order_number'=>$orderNumber
            ];

            $lists[]                    = $arr;
            $userIdArr[]              = $valUid;
        }
        $whereArr                      = [
            'company_id'=>$companyId, 'start_time'=>$startTime,'end_time'=>$endTime,
            'user_id_arr'=>$userIdArr
        ];
        $orderProcessListCollection  = OrderProcessCourse::getFormsOrderProessNumberList( $whereArr )->toArray();
        $userCountList                = [];
        foreach( $orderProcessListCollection as $key=>$val )
        {
            $valUid                    = $val->uid;
            $valProcessId             = $val->order_process_id;
            if( !in_array($valProcessId,$userCountList) )
            {
                $arr                    = [
                    'user_id'=>$valUid,'process_id'=>$valProcessId,
                ];
                $userCountList[]      = $arr;
            }
        }
        $processNumArr                = $uidIdArr = [];
        foreach( $userCountList as $key=>$val )
        {
            $userId                    = $val['user_id'];
            if( in_array($userId,$uidIdArr) )
            {
                $tempNumber                = $processNumArr[$userId] + 1;
                $processNumArr[$userId]  = $tempNumber;
            } else {
                $processNumArr[$userId]  = 1;
                $uidIdArr[]               = $userId;
            }
        }
        if( $processNumArr )
        {
            foreach( $lists as $key=>$val )
            {
                $userId                          = $val['user_id'];
                $orderNumber                     = $processNumArr[$userId];
                $lists[$key]['order_number']    = $orderNumber;
            }
        }
        $result             = [
            $lists, 'start_date'=>$startDate, 'end_date'=>$endDate
        ];
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $pageSize,
            'total'        => $count,
            'keyword'       => ''
        ];
        return $this->pagingData($lists, $pagingData);
    }
    public function courseList()
    {
        $headerUserId         = Func::getHeaderValueByName( 'userid' );
//        $headerUserId         = Func::getHeaderValueByName( 'userid', 1 );
        $headerInfo            = DepartmentUser::getCurrentInfo( $headerUserId );
        $headerCompanyId      = $headerInfo->company_id;

        $userId                = request( 'user_id');
//        $userId                = request( 'user_id', 9 );
        $userInfo              = DepartmentUser::getCurrentInfo( $userId );
        $companyId             = $userInfo->company_id;

        $pageSize               = request( 'pageSize', 10 );
        $page                   = request( 'page', 1 );
        $offset                 = ( $page - 1 ) * $pageSize;
        if( !$headerInfo || !$userInfo || $headerCompanyId != $companyId ){
            xThrow( 10006 );
        }

        $where                  = [
            'ygt_order_process_course.company_id'=>$companyId,
            'ygt_order_process_course.uid'=>$userId
        ];
        $orderby                = ['ygt_order_process_course.id','desc'];
        $fieldR                = 'ygt_order_process_course.order_process_id';
        $join                   = [
            ['table'=>'ygt_order_process as a', 'field_l'=>'a.id', 'field_c'=>'=', 'field_r'=>$fieldR],
            ['table'=>'ygt_order as b', 'field_l'=>'b.id', 'field_c'=>'=', 'field_r'=>'a.order_id'],
            ['table'=>'ygt_order_type as c', 'field_l'=>'c.id', 'field_c'=>'=', 'field_r'=>'b.order_type']
        ];
        $fields                 = 'c.title,b.order_title,b.created_at,b.finished_date,';
        $fields                 .= 'ygt_order_process_course.finish_number';
        $lists                  = OrderProcessCourse::getList( $where,$fields,$pageSize,$offset,$orderby,'',$join );
        $where                  = [
            'company_id'=>$companyId, 'uid'=>$userId
        ];
        $total                  = OrderProcessCourse::getCount( $where );
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $pageSize,
            'total'        => $total,
            'keyword'       => ''
        ];
        return $this->pagingData($lists, $pagingData);
    }
}
